package cn.interestingshop.service.user;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import cn.interestingshop.mapper.user.UserMapper;
import cn.interestingshop.utils.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;

import cn.interestingshop.dao.user.UserDao;
import cn.interestingshop.dao.user.UserDaoImpl;
import cn.interestingshop.entity.User;
import cn.interestingshop.utils.DataSourceUtil;

public class UserServiceImpl implements UserService {

    private Logger logger = Logger.getLogger(UserServiceImpl.class);

    @Override
    public boolean save(User user) {
        SqlSession session = null;
        boolean flag = false;
        try {
            session = MyBatisUtil.createSqlSession();
            int count = session.getMapper(UserMapper.class).save(user);
            if (count > 0) {
                flag = true;
                System.out.println("add success!");
                session.commit();
            } else {
                System.out.println("add failed!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            session.rollback();
        } finally {
            MyBatisUtil.closeSqlSession(session);
        }
        return flag;
    }

    @Override
    public boolean update(User user) {
        SqlSession session = null;
        boolean flag = false;
        try {
            session = MyBatisUtil.createSqlSession();
            int count = session.getMapper(UserMapper.class).update(user);
            if (count > 0) {
                flag = true;
                session.commit();
            }
        } catch (Exception e) {
            e.printStackTrace();
            session.rollback();
        } finally {
            MyBatisUtil.closeSqlSession(session);
        }
        return flag;
    }

    @Override
    public boolean deleteById(Integer userId) {
        SqlSession session = null;
        Boolean flag = false;
        try {
            session = MyBatisUtil.createSqlSession();
            int count = session.getMapper(UserMapper.class).deleteById(userId);
            if (count > 0) {
                flag = true;
                session.commit();
            } else {
            }
        } catch (Exception e) {
            e.printStackTrace();
            session.rollback();
        } finally {
            MyBatisUtil.closeSqlSession(session);
        }
        return flag;
    }

    @Override
    public User getById(Integer userId, String loginName) {
        SqlSession session = null;
        try {
            session = MyBatisUtil.createSqlSession();
            return session.getMapper(UserMapper.class).selectById(userId, loginName);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MyBatisUtil.closeSqlSession(session);
        }
        return null;
    }

    @Override
    public List<User> getList(Integer currentPageNo, Integer pageSize) {
        SqlSession session = null;
        try {
            session = MyBatisUtil.createSqlSession();
            return session.getMapper(UserMapper.class).selectList((currentPageNo-1)*pageSize, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MyBatisUtil.closeSqlSession(session);
        }
        return null;
    }

    @Override
    public int getCount() {
        SqlSession session = null;
        try {
            session = MyBatisUtil.createSqlSession();
            return session.getMapper(UserMapper.class).selectCount();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MyBatisUtil.closeSqlSession(session);
        }
        return 0;
    }
}
